<?php
class Msanpham extends CI_Model{
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }
    public function listall(){
        $this->db->order_by("sanpham_Id", "desc"); 
        $query=$this->db->get("sanpham");
        $result_array = $query->result_array();
        $i = 0;
        foreach ($result_array as $items) {
            $result_array[$i]['sanpham_Comment'] = $this->count_CommnetById($items['sanpham_Id']);
            $i++;
        }
        return $result_array; 
    }
    // lấy dữ liệu theo từng phần 
    public function list_with_page($number, $offset){
        $this->db->select('*');
        $this->db->from('sanpham');
        $this->db->join('typeproduct', 'sanpham_Loai = tp_Id');
        $this->db->limit($number,$offset);
        $this->db->order_by("sanpham_Id", "desc"); 
        $query =  $this->db->get(); 
        $result_array = $query->result_array();
        $i = 0;
        foreach ($result_array as $items) {
            $result_array[$i]['sanpham_Comment'] = $this->count_CommnetById($items['sanpham_Id']);
            $i++;
        }
        return $result_array; 
    } 
     
     // lấy dữ liệu theo từng phần 
    public function list_with_page_type($type, $number, $offset){
        $this->db->select('*');
        $this->db->from('sanpham');
        $this->db->join('typeproduct', 'sanpham_Loai = tp_Id');
        $this->db->limit($number,$offset);
        $this->db->where('sanpham_Loai', $type);
        $this->db->order_by("sanpham_Id", "desc");
        $query =  $this->db->get(); 
        $result_array = $query->result_array();

        $i = 0;
        foreach ($result_array as $items) {
            $result_array[$i]['sanpham_Comment'] = $this->count_CommnetById($items['sanpham_Id']);
            $i++;
        }
        return $result_array; 
    } 
    // đếm tổng số record trong table book 
    public function count_all(){ 
        return $this->db->count_all('sanpham'); 
    }

    public function count_all_type($type){ 
        $this->db->where('sanpham_Loai', $type);
        $this->db->from('sanpham');
        return $this->db->count_all_results(); 
    }
    public function get_One($id) {

        $this->db->select('*');
        $this->db->from('sanpham');
        $this->db->join('typeproduct', 'sanpham_Loai = tp_Id');
        $this->db->where('sanpham_Id', $id);
        $query = $this->db->get();
        $data = $query->row();
        $data->sanpham_Comment = $this->count_CommnetById($data->sanpham_Id);
        return $data;
    }
    
    public function insertProduct($data) {
        $this->db->insert('sanpham', $data);
    }

    public function updateProduct($data, $sanpham_Id) {
        $this->db->where('sanpham_Id', $sanpham_Id);
        $this->db->update('sanpham', $data); 
    }

    public function count_all_search($valueSearch){ 
        $this->db->like('sanpham_Ten', $valueSearch); 
        $this->db->from('sanpham');
        return $this->db->count_all_results(); 
    }

     // lấy dữ liệu theo từng phần 
    public function list_with_keySearch($valueSearch, $number, $offset){

        $this->db->select('*');
        $this->db->from('sanpham');
        $this->db->join('typeproduct', 'sanpham_Loai = tp_Id');
        $this->db->limit($number,$offset);
        $this->db->like('sanpham_Ten', $valueSearch);
        $this->db->order_by("sanpham_Id", "desc");
        $query =  $this->db->get(); 
        $result_array = $query->result_array();
        $i = 0;
        foreach ($result_array as $items) {
            $result_array[$i]['sanpham_Comment'] = $this->count_CommnetById($items['sanpham_Id']);
            $i++;
        }
        return $result_array; 
    } 

    public function count_CommnetById($cm_Product_Id) {
        $this->db->from('comment');
        $this->db->where('cm_Product_Id = ', $cm_Product_Id);

        return $this->db->count_all_results();
    }
}